import WXRealLogUtil from "../../../StartAb/WXRealLogUtil";
import WXUtil from "../../../StartAb/WXUtil";
import EventCenter from "../../TFrame/script/helper/EventCenter";
import Coordinates from "../Coordinates";
import EventNames from "../EventNames";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import PlayerDataMgr from "../PlayerDataMgr";
import ResManager from "../ResManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class DiffLoader extends cc.Component {

    @property(cc.Sprite)
    topSprite: cc.Sprite = null;

    @property({ type: cc.Sprite })
    bottomSprite: cc.Sprite = null;

    loadResPaths = []
    protected onLoad(): void {
        let bgResName = ResManager.getLevelResNameByIndex(ResManager.currentLevelIndex);
        let config = Coordinates.config[bgResName]
        if (config['allinone']) {
            this._loadDiffOnePic()
        } else {
            this._loadDiff5Pic()
        }
        ServiceLogic.action(ActionType.ActionEnterGame, {
            'level_name': (ResManager.currentLevelIndex + 1) + "/" + ResManager.levelResArray.length
        })
    }

    private _loadDiff5Pic() {
        let bgResName = ResManager.getLevelResNameByIndex(ResManager.currentLevelIndex);
        this.loadResPaths = [bgResName];
        for (let i = 0; i < ResManager.getDiffCount(); ++i) {
            this.loadResPaths.push(bgResName + "_" + i);
        }

        cc.assetManager.loadBundle("LevelAb", (err: Error, b: cc.AssetManager.Bundle) => {
            if (err) {
                WXUtil.retryCall(this._loadDiff5Pic, this)
                WXRealLogUtil.error(`加载资源出错:${err.message}`)
                return console.error(err.message);
            }
            b.load(this.loadResPaths, cc.SpriteFrame, (err: Error, sfs: cc.SpriteFrame[]) => {
                if (err) {
                    WXUtil.retryCall(this._loadDiff5Pic, this)
                    WXRealLogUtil.error(`加载资源出错:${err.message}`)
                    return console.error(err.message);
                }
                let resMap = {};
                sfs.forEach((sf: cc.SpriteFrame) => {
                    resMap[sf.name] = sf;
                })
                if (this.isValid) {
                    this.showBgSprite(resMap[bgResName])
                    this.showDiffes(resMap)
                    EventCenter.emit(EventNames.DiffShowDone);
                }
            })
        })
    }

    private _loadDiffOnePic() {
        let bgResName = ResManager.getLevelResNameByIndex(ResManager.currentLevelIndex);
        this.loadResPaths = [bgResName];
        this.loadResPaths.push(bgResName + "_b");
        cc.assetManager.loadBundle("LevelAb", (err: Error, b: cc.AssetManager.Bundle) => {
            if (err) {
                WXUtil.retryCall(this._loadDiffOnePic, this)
                WXRealLogUtil.error(`加载资源出错:${err.message}`)
                return console.error(err.message);
            }
            b.load(this.loadResPaths, cc.SpriteFrame, (err: Error, sfs: cc.SpriteFrame[]) => {
                if (err) {
                    WXUtil.retryCall(this._loadDiffOnePic, this)
                    WXRealLogUtil.error(`加载资源出错:${err.message}`)
                    return console.error(err.message);
                }
                let resMap = {};
                sfs.forEach((sf: cc.SpriteFrame) => {
                    resMap[sf.name] = sf;
                })
                if (this.isValid) {
                    this.showBgSprite(resMap[bgResName])
                    this.showDiffesInOnePic(resMap[bgResName + "_b"])
                    EventCenter.emit(EventNames.DiffShowDone);
                }
            })
        })
    }

    protected onDestroy(): void {
        this.loadResPaths.forEach((path) => {
            cc.resources.release(path, cc.SpriteFrame)
            cc.resources.release(path, cc.JsonAsset)
        })
    }

    protected showBgSprite(sf: cc.SpriteFrame) {
        this.topSprite.spriteFrame = sf;
        this.bottomSprite.spriteFrame = sf;
    }

    protected showDiffes(sfMap) {
        let bgResName = ResManager.getLevelResNameByIndex(ResManager.currentLevelIndex);
        for (let i = 0; i < ResManager.getDiffCount(); ++i) {
            let diffName = bgResName + "_" + i;
            let sf = sfMap[diffName] as cc.SpriteFrame;
            let diffNode = new cc.Node(i.toString())
            let spr = diffNode.addComponent(cc.Sprite)
            spr.spriteFrame = sf
            spr.sizeMode = cc.Sprite.SizeMode.RAW
            spr.trim = false
            diffNode.parent = this.bottomSprite.node
            let sizeP = this.bottomSprite.node.getContentSize()
            diffNode.position = cc.v3(Coordinates.config[bgResName][diffName].x, Coordinates.config[bgResName][diffName].y);
        }
    }

    protected showDiffesInOnePic(sf: cc.SpriteFrame) {
        let diffNode = new cc.Node(sf.name)
        let spr = diffNode.addComponent(cc.Sprite)
        spr.spriteFrame = sf
        spr.sizeMode = cc.Sprite.SizeMode.RAW
        spr.trim = false
        diffNode.parent = this.bottomSprite.node
        diffNode.position = cc.v3(0, 0, 0);
        spr.srcBlendFactor = cc.macro.BlendFactor.ONE
        spr.dstBlendFactor = cc.macro.BlendFactor.ONE_MINUS_SRC_ALPHA
    }
}
